<?php

namespace app\api1\controller;
use hg\apidoc\annotation as Apidoc;
class RobotEvent extends Auth
{
    /**
     * @Apidoc\Title("新增机器人事件")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("trigger_text", type="string", require=true, desc="触发文本")
     * @Apidoc\Param("form_field", type="array", require=true, desc="表单字段")
     * @Apidoc\Param("robot_id", type="int", require=true, desc="机器人id")
     * @Apidoc\Param("name", type="string", require=true, desc="事件名称")
     * @Apidoc\Param("form_name", type="string", require=true, desc="表单名称")
     * @Apidoc\Param("type", type="int", require=true, desc="类型")
     * @Apidoc\Returned("data", type="object", desc="返回数据")
     */
    public function save()
    {
        $member_id = $this->request->uid;
        $trigger_text = input('trigger_text', '', 'trim');
        if(!$trigger_text){
            return error('触发文本不能为空！');
        }
        $form_field = input('form_field', []);
        if(!$form_field){
            return error('表单字段不能为空！');
        }
        $robot_id = input('robot_id', 0, 'intval');
        if(!$robot_id){
            return error('机器人id不能为空！');
        }
        $robot = \app\common\model\Robot::where('id', $robot_id)
            ->where('member_id', $member_id)
            ->find();
        if(!$robot){
            return error('该机器人不存在！');
        }
        $name = input('name', '', 'trim');
        if(!$name){
            return error('事件名称不能为空！');
        }
        $form_name = input('form_name', '', 'trim');
        if(!$form_name){
            return error('表单名称不能为空！');
        }
        $type = input('type', 0, 'trim');
        $id = input('id', 0, 'intval');

        if($id){
            $res = \app\common\model\RobotEvent::where('id', $id)->find();
            \app\common\model\RobotEvent::where('id', $id)->update([
                'trigger_text' => $trigger_text,
                'form_field' => $form_field,
                'robot_id' => $robot_id,
                'name' => $name,
                'form_name' => $form_name,
                'type' => $type,
                'is_train'=> 0,
            ]);
            \app\common\model\Robot::where('id', $robot_id)->update(['is_event' => 2]);
            return success();
        }

        \app\common\model\RobotEvent::create([
            'robot_id' => $robot_id,
            'trigger_text' => $trigger_text,
            'form_field' => $form_field,
            'name' => $name,
            'form_name' => $form_name,
            'type' => $type,
        ]);
        \app\common\model\Robot::where('id', $robot_id)->update(['is_event' => 2]);
        return success();

    }

    /**
     * @Apidoc\Title("机器人事件列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", require=true, desc="事件id")
     * @Apidoc\Returned("data", type="object", desc="返回数据")
     */
    public function list()
    {
        $robot_id = input('robot_id', 0, 'intval');
        $member_id = $this->request->uid;
        $robot = \app\common\model\Robot::where('id', $robot_id)
            ->where('member_id', $member_id)
            ->find();
        if(!$robot){
            return error('该机器人不属于你，非法请求！' );
        }
        $res = \app\common\model\RobotEvent::where('robot_id', $robot_id)->select();
        return success('ok', $res);
    }

    /**
     * @Apidoc\Title("机器人事件详情")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", require=true, desc="事件id")
     * @Apidoc\Param("robot_id", type="int", require=true, desc="机器人id")
     * @Apidoc\Returned("data", type="object", desc="返回数据")
     */
    public function delete()
    {
        $robot_id = input('robot_id', 0, 'intval');
        $member_id = $this->request->uid;
        $robot = \app\common\model\Robot::where('id', $robot_id)
            ->where('member_id', $member_id)
            ->find();
        if(!$robot){
            return error('该机器人不属于你，非法请求！' );
        }
        $event = \app\common\model\RobotEvent::where('id', input('id', 0, 'intval'))
            ->where('robot_id', $robot_id)
            ->find();
        if(!$event){
            return error('该事件不存在！');
        }
        $event->delete();
        \app\common\model\Robot::where('id', $robot_id)->update(['is_event' => 2]);
        return success();
    }


}